package org.ruoyi.alibaba.chat.service;


import com.alibaba.cloud.ai.dashscope.embedding.DashScopeEmbeddingOptions;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.EmbeddingRequest;
import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.ai.embedding.EmbeddingResponseMetadata;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * rag 服务
 */

@Service
public class AlibabaRagService {

    private final EmbeddingModel embeddingModel;



    @Autowired
    public AlibabaRagService(EmbeddingModel embeddingModel) {
        this.embeddingModel = embeddingModel;
    }

    public void rag(){
        // 构建向量请求
        EmbeddingRequest request = new EmbeddingRequest(List.of("Hello World", "World is big and salvation is near"),
                DashScopeEmbeddingOptions.builder()
                        .withModel("text-embedding-v3")
                        .build());


        // 获取请求响应
        EmbeddingResponse embeddingResponse = embeddingModel.call(request);

        // 获取向量
        embeddingResponse.getResults().forEach(System.out::println);

        // h获取向量元数据
        EmbeddingResponseMetadata metadata = embeddingResponse.getMetadata();
        System.out.println(metadata);
    }
}
